﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using WebUI.DAL;

namespace WebUI.BL.Catalog
{
    public class CatalogBL
    {
        public void clsCataLogInsert(tbl_Catalog catalogs)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_Catalog catalog = new tbl_Catalog();
                    catalog.CatalogName = catalogs.CatalogName;
                    catalog.CatalogLink = catalogs.CatalogLink;
                    catalog.Description = catalogs.Description;
                    catalog.CreatedUser = catalogs.CreatedUser;
                    catalog.CreatedDate = DateTime.Now;
                    catalog.Status = catalogs.Status;

                    adlerContextDb.tbl_Catalogs.InsertOnSubmit(catalog);
                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        public void clsCataLogModify(tbl_Catalog catalogs)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var catalog = adlerContextDb.tbl_Catalogs.Single(d => d.CatalogNo == catalogs.CatalogNo);
                    catalog.CatalogName = catalogs.CatalogName;
                    catalog.CatalogLink = catalogs.CatalogLink;
                    catalog.Description = catalogs.Description;
                    catalog.Status = catalogs.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        public void clsCataLogDelete(tbl_Catalog catalogs)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var catalog = adlerContextDb.tbl_Catalogs.Single(d => d.CatalogNo == catalogs.CatalogNo);
                    catalog.Status = catalogs.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        // Show Catalog.
        public List<MODEL_SP_ADMIN_SEARCH_CATALOG> Get_Admin_Catalog(string catalogname, string status)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                int index = 1;
                //var query = from n in contextdb.SP_ADMIN_SEARCH_CATALOG(catalogname, status)
                //            select new MODEL_SP_ADMIN_SEARCH_CATALOG
                //            {
                //                Index = index++,
                //                CatalogNo = n.CatalogNo,
                //                CatalogName = n.CatalogName,
                //                CatalogLink = n.CatalogLink,
                //                Description = n.Description,
                //                CreatedUser = n.CreatedUser,
                //                CreatedDate = n.CreatedDate,
                //                Status=n.Status
                //            };
                return null;//query.ToList();
            }
        }

        public tbl_Catalog GetCatalogByNo(int catalogno)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                return contextdb.tbl_Catalogs.Single(d => d.CatalogNo == catalogno);
            }
        }
    }
}
